package com.zy.springboot_nanhua4.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.springboot_nanhua4.configs.OssConfig;
import com.zy.springboot_nanhua4.dao.StudentMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.springboot_nanhua4.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class StudentServiceImpl
        extends ServiceImpl<StudentMapper, Student>
        implements StudentService{
    @Autowired
    private OSS oss;
    @Autowired
    private OssConfig ossConfig;


    @Override
    public Student add(Student s) {
        // 加入OSS上传图片功能，成功后OSS会返回一个Url地址，将其设置为s的photo属性中
        MultipartFile photoFile=s.getPhotofile();
        String originalFilename = photoFile.getOriginalFilename();
        String newfileName= UUID.randomUUID().toString().replace("-","")+"_"+originalFilename;
        String newFilePath="student/"+newfileName;
        try{
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),newFilePath,photoFile.getInputStream());
            oss.putObject(putObjectRequest);
            s.setPhoto(ossConfig.getUrlPrefix()+newFilePath);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException("上传图片到OSS失败");
        }
        s.setPhotofile(null);
        // TOOD：再插入数据库中即可。
        int r=this.baseMapper.insert(s);
        if(r>0){
            return baseMapper.selectById(s.getId());
        }
        throw new RuntimeException("添加学生失败");
    }

    @Override
    public void delete(int id) {
        int r=this.baseMapper.deleteById(id);
        if(r<=0){
            throw new RuntimeException("删除学生失败");
        }
    }

    @Override
    public Student find(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Student> findList() {
        return baseMapper.selectList(null);
    }

    @Override
    public Student update(Student s) {
        if(s.getPhotofile()==null || s.getPhotofile().isEmpty()){
            Student oldstudent=baseMapper.selectById(s.getId());
            s.setPhoto(oldstudent.getPhoto());
            int r=this.baseMapper.updateById(s);
            if(r>0){
                return baseMapper.selectById(s.getId());
            }
            throw new RuntimeException("更新无图学生失败");
        }
        MultipartFile photoFile=s.getPhotofile();

        String originalFilename = photoFile.getOriginalFilename();
        String newfileName= UUID.randomUUID().toString().replace("-","")+"_"+originalFilename;
        String newFilePath="student/"+newfileName;
        try{
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(),newFilePath,photoFile.getInputStream());
            oss.putObject(putObjectRequest);
            s.setPhoto(ossConfig.getUrlPrefix()+newFilePath);
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException("上传图片到OSS失败");
        }
        s.setPhotofile(null);
        int r=this.baseMapper.updateById(s);
        if(r>0){
            return baseMapper.selectById(s.getId());
        }
        throw new RuntimeException("更新学生失败");
    }

    @Override
    public List<Student>findByName(String name){
        if(name!=null&& !"".equals(name)){
            return baseMapper.selectList(new LambdaQueryWrapper<Student>().like(Student::getName,name));
        }
        return this.findList();
    }
}
